<?php
namespace Home\Controller;

class PatientController extends CommonController{
    
    //医生查看患者主页
    public function index($id){
        $vo = M('User')->find($id);
        $hptRow = M('Hospital')->find($vo['hpt_id']);
        $vo['htp_name'] = $hptRow['name'];
        $this->assign('vo',$vo);
        
        $this->assign('userInfo',M('User')->find($this->uid));
        $this->display();
    }
    
    //患者列表
    public function lists(){
        //用户信息
        $User = M('User');
        $userInfo = $User->find($this->uid);
        $field = $userInfo['type'] == '1' ? 'uid' : 'nid';//医生为uid字段营养师为nid
            
        $Weight = M('Weight');
        $where = array();
        $where[$field] = $this->uid;
        $where['name'] = array('neq','');
        if(I('get.keywords')){
            $where['name'] = array('like','%'.I('get.keywords').'%');
        }
        
        $list = $User->where($where)->select();
        if($userInfo['type'] == '1'){
            foreach($list as $k=>$v){
                $weightRow = $Weight->where(array($field=>$v['id']))->order('id desc')->find();
                if($weightRow['vol'] > $v['weight_safe']) $list[$k]['is_over'] = 1;//超重
            }
        }
        $this->assign('list',$list);
        
        $this->assign('userInfo',$userInfo);
        $this->display();
    }
    
    //患者消息列表
    public function msg(){
        //我的个人信息
        $userInfo = M('User')->find($this->uid);
        $this->assign('userInfo',$userInfo);
        $field = $userInfo['type'] == '1' ? 'uid' : 'nid';//医生为uid字段营养师为nid
        
        //我的患者列表
        $where = array();
        $where[$field] = $this->uid;
        $where['name'] = array('neq','');
        if(I('get.keywords')){
            $where['name'] = array('like','%'.I('get.keywords').'%');
        }
        $list = M('User')->where($where)->select();
        
        //查询是否有未读信息
        $Chat = M('Chat');
        $result = array();
        foreach($list as $k=>$v){
            $arr = array();
            $arr = $Chat->where(array('sender'=>$v['phone'],'receiver'=>$userInfo['phone'],'has_read'=>'0'))->order('id desc')->select();
            if($arr){
                $result[$k] = $v;
                $result[$k]['msg_num'] = count($arr);
                $result[$k]['msg_content'] = $arr[0]['content'];
            }
        }
        $this->assign('list',$result);
        $this->display();
    }
    
    //设置安全体重值
    public function saveWeightSafe(){
        M('User')->save(array('id'=>I('post.id'),'weight_safe'=>I('post.weight_safe')));
        if(I('post.id')) D('Score')->setSafeWeight($this->uid,I('post.id'));
    }
    
    //群发消息
    public function mass(){
        //我的个人信息
        $userInfo = M('User')->find($this->uid);
        $field = $userInfo['type'] == '1' ? 'uid' : 'nid';//医生为uid字段营养师为nid
        
        //我的患者列表
        $where = array();$ids = array();
        $where[$field] = $this->uid;
        $where['name'] = array('neq','');
        $list = M('User')->where($where)->select();
        
        $Chat = M('Chat');
        $sender = I('post.phone');
        $content = I('post.content');
        if($list && $sender && $content){
            foreach($list as $v){
                $Chat->add(array('sender'=>$sender,'receiver'=>$v['phone'],'content'=>$content));
            }
        }
        die('suc');
    }
    
}